import VanaTgweb from "./vana_tgweb";

export class VanaManager {
  constructor(parameters) {
    this.isRunning = false
  }

  static _shared = null;
  static shared() {
    if (this._shared == null) {
      this._shared = new VanaManager()
    }
    return this._shared;
  }

  async launch() {
    this.isRunning = true
    await this.launchTgweb().then(res => {
      this.launchGame()
    }).catch(error => {
      console.error(error)
      this.isRunning = false
    })
  }

  launchGame() {
    const worker = new Worker('./vana_game.js', { type: 'module' })
    this.worker = worker
    const self = this
    worker.onerror = (error) => {
      console.error(error)
      if (self.worker != null) {
        self.worker.terminate()
        self.worker = null
        self.isRunning = false
      }
    }
  }

  async launchTgweb() {
    this.tgweb = new VanaTgweb()
    await this.tgweb.allRun()
  }

  isGameRunning() {
    return this.isRunning;
  }
}